package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FolderAdapter;
import flc.ast.bean.MyPrivacyBean;
import flc.ast.databinding.ActivityPrivacyFileBinding;
import flc.ast.dialog.CreateFolderDialog;
import flc.ast.dialog.InputAnswerDialog;
import flc.ast.dialog.InputPasswordDialog;
import flc.ast.dialog.SetAnswerDialog;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;

public class PrivacyFileActivity extends BaseAc<ActivityPrivacyFileBinding> {

    private List<String> listShow = new ArrayList<>();
    private List<MyPrivacyBean> listPassword = new ArrayList<>();
    private FolderAdapter folderAdapter;
    private String folderName, password;
    private int selPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_privacy_file;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPrivacyFileBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvFolderList.setLayoutManager(new GridLayoutManager(mContext, 4));
        folderAdapter = new FolderAdapter();
        mDataBinding.rvFolderList.setAdapter(folderAdapter);
        folderAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        selPosition = position;
        if (position == listShow.size() - 1) {
            showCreateFolder();
        } else {
            showInputPassword();
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myPrivacy;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getName());
            }
        }
        listShow.add("");
        listPassword = AppSpUtils.getPrivacyList();
        folderAdapter.setList(listShow);
    }

    private void showCreateFolder() {
        CreateFolderDialog dialog = new CreateFolderDialog(mContext);

        dialog.setListener(new CreateFolderDialog.Ilistener() {
            @Override
            public void clickAddFolder(String name, String psw) {
                folderName = name;
                password = psw;
                showSetAnswer();
            }
        });

        dialog.show();
    }

    private void showSetAnswer() {
        SetAnswerDialog dialog = new SetAnswerDialog(mContext);

        dialog.setListener(new SetAnswerDialog.Ilistener() {
            @Override
            public void clickAddFolder(String question, String answer) {
                createFolder(question, answer);
            }
        });

        dialog.show();
    }

    private void showInputPassword() {
        InputPasswordDialog dialog = new InputPasswordDialog(mContext);

        dialog.name = listShow.get(selPosition);
        dialog.password = listPassword.get(selPosition).getPassword();

        dialog.setListener(new InputPasswordDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                FolderActivity.folderName = listShow.get(selPosition);
                startActivity(FolderActivity.class);
            }

            @Override
            public void clickForget() {
                showInputAnswer();
            }
        });

        dialog.show();
    }

    private void showInputAnswer() {
        InputAnswerDialog dialog = new InputAnswerDialog(mContext);

        dialog.question = listPassword.get(selPosition).getQuestion();
        dialog.answer = listPassword.get(selPosition).getAnswer();

        dialog.setListener(new InputAnswerDialog.Ilistener() {
            @Override
            public void clickConFirm() {
                FolderActivity.folderName = listShow.get(selPosition);
                startActivity(FolderActivity.class);
            }
        });

        dialog.show();
    }

    private void createFolder(String question, String answer) {
        String path = PathUtils.getFilesPathExternalFirst() + Content.myPrivacy + "/" + folderName;
        if (FileUtils.isFileExists(path)) {
            ToastUtils.showShort(getString(R.string.folder_is_exist));
        } else {
            FileUtils.createOrExistsDir(path);
            //准备数据
            List<MyPrivacyBean> listSave = new ArrayList<>();
            listSave.add(new MyPrivacyBean(password, question, answer));
            //保存数据
            List<MyPrivacyBean> listSp = AppSpUtils.getPrivacyList();
            if (listSp != null && listSp.size() > 0) {
                listSp.addAll(listSave);
                AppSpUtils.savePrivacyList(listSp);
            } else {
                AppSpUtils.savePrivacyList(listSave);
            }
            getData();
            ToastUtils.showShort(getString(R.string.folder_add_suc));
        }
    }

}